function homeAnimationEntry() {
    // 首页小球相关动画
    homeAnimationCircles();
    // 首页字体相关动画
    homeAnimationFonts();
    // 首页3D相关动画
    homeAnimation3D();
}

// 和小球有关的动画控制
function homeAnimationCircles() {
    homeAnimationCircle1();
    homeAnimationCircle2();
}

// 和字体有关的动画控制
function homeAnimationFonts() {
    // 拆分的描述字符串
    const mySplitText = new SplitText(".home-left__desc", { type: "chars" });
    const chars = mySplitText.chars;

    const mySplitTextEn = new SplitText(".home-left__desc-en", { type: "chars" });
    const charsEn = mySplitTextEn.chars;

    TweenMax.from('.home-left__personal-info', 0.8, { opacity: 0, top: 50, delay: 2 })

    const timeline = new TimelineMax({ repeat: -1 });
    timeline.add(TweenMax.staggerFrom(chars, 1.6, { opacity: 0, top: 20, ease: Back.easeInOut }, 0.01))
    timeline.add(TweenMax.from('.home-left__title', 0.8, { opacity: 0, top: 50, ease: Back.easeOut, delay: -2 }))
        // 8秒后切换英文模式
    timeline.add(TweenMax.to('.home-left__title', 0.8, { opacity: 0, top: 50, ease: Back.easeOut, delay: 8 }))
    timeline.add(TweenMax.to('.home-left__desc', 0.4, { opacity: 0, top: 50, ease: Back.easeOut, delay: -0.6 }))
    timeline.add(TweenMax.staggerFrom(charsEn, 1.6, { opacity: 0, top: 20, ease: Back.easeInOut }, 0.01))
    timeline.add(TweenMax.to('.home-left__title-en', 0.8, { opacity: 1, top: 0, ease: Back.easeOut, delay: -2 }))
    timeline.add(TweenMax.to('.home-left__title', 8, {}))
}

// 首页和3D相关的动画控制
function homeAnimation3D() {
    // 定义和three有关的全局变量
    let scene, camera,
        renderer,
        container,
        controls;

    // 设备类型
    const deviceType = isPC() ? 'PC' : 'MOBILE';

    // 画布宽高
    let width, height

    let newTime = new Date().getTime();
    let oldTime = new Date().getTime();

    let mousePos = { x: 0, y: 0 };

    let game = {
        speed: 0,
        initSpeed: .00035,
        baseSpeed: .00035,
        targetBaseSpeed: .00035,
        incrementSpeedByTime: .0000025,
        incrementSpeedByLevel: .000005,
        distanceForSpeedUpdate: 100,
        speedLastUpdate: 0,

        distance: 0,
        ratioSpeedDistance: 50,
        energy: 100,
        ratioSpeedEnergy: 3,

        level: 1,
        levelLastUpdate: 0,
        distanceForLevelUpdate: 1000,

        planeDefaultHeight: 100,
        planeAmpHeight: 80,
        planeAmpWidth: 75,
        planeMoveSensivity: 0.005,
        planeRotXSensivity: 0.0008,
        planeRotZSensivity: 0.0004,
        planeFallSpeed: .001,
        planeMinSpeed: 1.2,
        planeMaxSpeed: 1.6,
        planeSpeed: 0,
        planeCollisionDisplacementX: 0,
        planeCollisionSpeedX: 0,

        planeCollisionDisplacementY: 0,
        planeCollisionSpeedY: 0,

        seaRadius: 600,
        seaLength: 800,
        //seaRotationSpeed:0.006,
        wavesMinAmp: 5,
        wavesMaxAmp: 20,
        wavesMinSpeed: 0.001,
        wavesMaxSpeed: 0.003,

        cameraFarPos: 500,
        cameraNearPos: 150,
        cameraSensivity: 0.002,

        coinDistanceTolerance: 15,
        coinValue: 3,
        coinsSpeed: .5,
        coinLastSpawn: 0,
        distanceForCoinsSpawn: 100,

        ennemyDistanceTolerance: 10,
        ennemyValue: 10,
        ennemiesSpeed: .6,
        ennemyLastSpawn: 0,
        distanceForEnnemiesSpawn: 50,

        status: "playing",
    };

    init();
    loop();

    function init() {
        createScene();
        createLights();
        createWorld();

        document.addEventListener('mousemove', handleMouseMove, false);
        document.addEventListener('touchmove', handleTouchMove, false);
    }

    function loop() {
        newTime = new Date().getTime();
        deltaTime = newTime - oldTime;
        oldTime = newTime;

        updatePlane(game.speed, deltaTime);
        game.baseSpeed += (game.targetBaseSpeed - game.baseSpeed) * deltaTime * 0.02;
        game.speed = game.baseSpeed * game.planeSpeed;
        airplane.propeller.rotation.x += .2 + game.planeSpeed * deltaTime * .005;

        renderer.render(scene, camera);

        requestAnimationFrame(loop);
    }

    function createScene() {
        width = $('.home-right__3d').width() || 600;
        height = $('.home-right__3d').height() || 400;

        scene = new THREE.Scene();
        camera = new THREE.PerspectiveCamera(50, width / height, 0.1, 1000);

        if (deviceType === 'PC') {
            camera.position.z = 40;
            camera.position.x = -90;
            camera.position.y = 500;
        } else {
            camera.position.z = 70;
            camera.position.x = -70;
            camera.lookAt(new THREE.Vector3(-66, 20, 0));
        }

        renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true });
        renderer.setSize(width, height);
        renderer.shadowMap.enabled = true;

        container = document.querySelector('.home-right__3d');
        container.appendChild(renderer.domElement);
    }

    function createLights() {
        hemisphereLight = new THREE.HemisphereLight(0xaaaaaa, 0x000000, .9)

        ambientLight = new THREE.AmbientLight(0xdc8874, .5);

        shadowLight = new THREE.DirectionalLight(0xffffff, .9);
        shadowLight.position.set(150, 350, 350);
        shadowLight.castShadow = true;
        shadowLight.shadow.camera.left = -400;
        shadowLight.shadow.camera.right = 400;
        shadowLight.shadow.camera.top = 400;
        shadowLight.shadow.camera.bottom = -400;
        shadowLight.shadow.camera.near = 1;
        shadowLight.shadow.camera.far = 1000;
        shadowLight.shadow.mapSize.width = 4096;
        shadowLight.shadow.mapSize.height = 4096;

        // var ch = new THREE.CameraHelper(shadowLight.shadow.camera);

        // scene.add(ch);
        scene.add(hemisphereLight);
        scene.add(shadowLight);
        scene.add(ambientLight);
    }

    function createWorld() {
        airplane = new AirPlane();
        airplane.mesh.scale.set(.25, .25, .25);
        if (deviceType === 'PC') {
            airplane.mesh.rotation.y = 3.4;
        } else {
            airplane.mesh.rotation.y = 4.0;
        }

        scene.add(airplane.mesh);
    }

    function updatePlane() {

        game.planeSpeed = normalize(mousePos.x, -.5, .5, game.planeMinSpeed, game.planeMaxSpeed);
        var targetY = normalize(mousePos.y, -.75, .75, game.planeDefaultHeight - game.planeAmpHeight, game.planeDefaultHeight + game.planeAmpHeight);
        var targetX = normalize(mousePos.x, -1, 1, -game.planeAmpWidth * .7, -game.planeAmpWidth);

        game.planeCollisionDisplacementX += game.planeCollisionSpeedX;
        targetX += game.planeCollisionDisplacementX;


        game.planeCollisionDisplacementY += game.planeCollisionSpeedY;
        targetY += game.planeCollisionDisplacementY;

        airplane.mesh.position.y += (targetY - airplane.mesh.position.y) * deltaTime * game.planeMoveSensivity;
        airplane.mesh.position.x += (targetX - airplane.mesh.position.x) * deltaTime * game.planeMoveSensivity;

        airplane.mesh.rotation.z = (targetY - airplane.mesh.position.y) * deltaTime * game.planeRotXSensivity;
        airplane.mesh.rotation.x = (airplane.mesh.position.y - targetY) * deltaTime * game.planeRotZSensivity;
        var targetCameraZ = normalize(game.planeSpeed, game.planeMinSpeed, game.planeMaxSpeed, game.cameraNearPos, game.cameraFarPos);
        camera.fov = normalize(mousePos.x, -1, 1, 40, 80);
        camera.updateProjectionMatrix()
        camera.position.y += (airplane.mesh.position.y - camera.position.y) * deltaTime * game.cameraSensivity;

        game.planeCollisionSpeedX += (0 - game.planeCollisionSpeedX) * deltaTime * 0.03;
        game.planeCollisionDisplacementX += (0 - game.planeCollisionDisplacementX) * deltaTime * 0.01;
        game.planeCollisionSpeedY += (0 - game.planeCollisionSpeedY) * deltaTime * 0.03;
        game.planeCollisionDisplacementY += (0 - game.planeCollisionDisplacementY) * deltaTime * 0.01;

        airplane.pilot.updateHairs();
    }

    function normalize(v, vmin, vmax, tmin, tmax) {
        var nv = Math.max(Math.min(v, vmax), vmin);
        var dv = vmax - vmin;
        var pc = (nv - vmin) / dv;
        var dt = tmax - tmin;
        var tv = tmin + (pc * dt);
        return tv;
    }

    function handleMouseMove(event) {
        var tx = -1 + (event.clientX / width) * 2;
        var ty = 1 - (event.clientY / height) * 2;
        mousePos = { x: tx, y: ty };
    }

    function handleTouchMove(event) {
        var tx = 1 - (event.touches[0].pageX / width) * 2;
        var ty = 1 - (event.touches[0].pageY / height) * 2;
        mousePos = { x: tx, y: ty };
    }
}

function isPC() {
    let userAgentInfo = navigator.userAgent;
    let agentList = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];

    let flag = true;
    for (let i = 0; i < agentList.length; i++) {
        if (userAgentInfo.indexOf(agentList[i]) > 0) {
            flag = false;
            break;
        }
    }
    return flag;
}

// 和小球1有关的动画控制
function homeAnimationCircle1() {
    const timeline = new TimelineMax({ repeat: -1, yoyo: true });
    timeline.add(TweenMax.to('.home-left__background-circle1', 8, { left: '12vw', top: '28vh', filter: 'blur(70px)', ease: Back.easeOut }))
    timeline.add(TweenMax.to('.home-left__background-circle1', 4, { left: '20vw', top: '22vh', filter: 'blur(100px)', ease: Back.easeOut }))
}

// 和小球2有关的动画控制
function homeAnimationCircle2() {
    const timeline = new TimelineMax({ repeat: -1, yoyo: true });
    timeline.add(TweenMax.to('.home-left__background-circle2', 6, { left: '16vw', top: '44vh', ease: Back.easeOut }))
    timeline.add(TweenMax.to('.home-left__background-circle2', 8, { left: '24', top: '22vh', filter: 'blur(50px)', ease: Back.easeOut }))
}